import React, {Component} from 'react';
import axios from 'axios';
import JsonUtils from './JsonUtils';
import { Feedback } from '@icedesign/base';
import $ from 'jquery';
import 'es6-promise';
//参考 https://blog.csdn.net/qq_35844177/article/details/78809499

/**
*  ajax数据请求
*/
export default class AxiosUtils extends Component {
  static get(url, json) {
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params: JsonUtils.jsonToParam(
          json == undefined
          ? "{}"
          : json, {"dataType": "json"})
      }).then(res => {
        //验证是否登录超时
        if(res.data.code == "998"){
          // 登录成功后做对应的逻辑处理
          window.location.href='#/login'
          return;
        } else if (res.data.code != "000") {
          Feedback.toast.error(res.data.msg != undefined?res.data.msg:"系统异常！");
          return;
        }
        resolve(res.data)
      }).catch(err => {
        reject(err)
      })
    })
  }

  static post(url, json) {
    return new Promise((resolve, reject) => {
      axios.post(url, JsonUtils.jsonToParam(
        json == undefined
        ? "{}"
        : json), {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }).then(res => {
        //验证是否登录超时
        if(res.data.code == "998"){
          // 登录成功后做对应的逻辑处理
          window.location.href='#/login'
          return;
        } else if (res.data.code != "000") {
          Feedback.toast.error(res.data.msg != undefined?res.data.msg:"系统异常！");
          return;
        }
        resolve(res.data)
      }).catch(err => {
        reject(err)
      })
    })
  }

  static postNotFeed(url, json) {
    return new Promise((resolve, reject) => {
      axios.post(url, JsonUtils.jsonToParam(
        json == undefined
        ? "{}"
        : json), {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }).then(res => {
        //验证是否登录超时
        if(res.data.code == "998"){
          // 登录成功后做对应的逻辑处理
          window.location.href='#/login'
          return;
        }
        resolve(res.data)
      }).catch(err => {
        reject(err)
      })
    })
  }

  static ajax(type,url,json){
    let result;
    $.ajax({
      url: url,
      data:json,
      dataType: "json",
      cache: false,
      async: false,
      type: type,
      success: function (res) {
        //验证是否登录超时
        if(res.code == "998"){
          // 登录成功后做对应的逻辑处理
          window.location.href='#/login';
          return;
        } else if (res.code != "000") {
          Feedback.toast.error(res.msg != undefined?res.msg:"系统异常！");
          return;
        }
        result = res;
      },error: function(){
        Feedback.toast.error("系统异常！");
      }
    });
    return result;
  }

  static ajaxNotFeed(type,url,json){
    let result;
    $.ajax({
      url: url,
      data:json,
      dataType: "json",
      cache: false,
      async: false,
      type: type,
      success: function (res) {
        //验证是否登录超时
        if(res.code == "998"){
          // 登录成功后做对应的逻辑处理
          window.location.href='#/login';
          return;
        }
        result = res;
      },error: function(){
        Feedback.toast.error("系统异常！");
      }
    });
    return result;
  }
}
